using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Plugin.Sns.Scene;
using MicroMsg.Storage;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Xml.Linq;
namespace MicroMsg.Manager
{
	public class NewsMgr
	{
		private const string TAG = "NewsMgr";
		public const int OBJID = 1;
		private EventWatcher NewsModifyWatcher;
		public NewsMgr()
		{
			this.init();
			ChatMsgMgr.registerCleanMsg(new Action(NewsMgr.NewsMsgClearAll));
		}
		private void init()
		{
			this.NewsModifyWatcher = new EventWatcher(this, null, new EventHandlerDelegate(this.onNewsChange));
			EventCenter.registerEventWatcher(EventConst.ON_STORAGE_NEWSMSG_ADD, this.NewsModifyWatcher);
			EventCenter.registerEventWatcher(EventConst.ON_STORAGE_NEWSMSG_DEL, this.NewsModifyWatcher);
		}
		private void uninit()
		{
			if (this.NewsModifyWatcher != null)
			{
				EventCenter.removeEventWatcher(EventConst.ON_STORAGE_NEWSMSG_ADD, this.NewsModifyWatcher);
				EventCenter.removeEventWatcher(EventConst.ON_STORAGE_NEWSMSG_DEL, this.NewsModifyWatcher);
				this.NewsModifyWatcher = null;
			}
		}
		public static ObservableCollection<AppMsgInfo> getList(int start, int int_0)
		{
			List<AppMsgInfo> newsListFromChat = NewsMgr.getNewsListFromChat(start, int_0);
			if (newsListFromChat == null)
			{
				return null;
			}
			return new ObservableCollection<AppMsgInfo>(newsListFromChat);
		}
		public static List<AppMsgInfo> getNewsListFromChat(int start, int int_0)
		{
			List<AppMsgInfo> list = new List<AppMsgInfo>();
			List<ChatMsg> msgList = StorageMgr.chatMsg.getMsgList("newsapp", start, int_0);
			if (msgList == null)
			{
				return null;
			}
			using (List<ChatMsg>.Enumerator enumerator = msgList.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					ChatMsg current = enumerator.get_Current();
					MMReaderInfo mMReaderInfo = AppMsgMgr.parseMMReaderXml(current.strMsg);
					if (mMReaderInfo != null)
					{
						if (mMReaderInfo.items != null && mMReaderInfo.items.get_Count() == 1 && mMReaderInfo.items.get_Item(0).string_0.Contains("yd.weixin.qq.com/static/html/news.html") && string.IsNullOrEmpty(mMReaderInfo.items.get_Item(0).cover))
						{
							mMReaderInfo.items.get_Item(0).cover = "http://yd.weixin.qq.com/static/html/news.png";
						}
						list.Add(new AppMsgInfo
						{
							mmreader = mMReaderInfo,
							nChatMsgLocalID = current.nMsgLocalID,
							itemType = 1
						});
					}
				}
			}
			return list;
		}
		public static snsMediaDownloadInfo getNewsImg(string imgUrl)
		{
			snsMediaDownloadInfo snsMediaDownloadInfo = new snsMediaDownloadInfo();
			snsMediaDownloadInfo.priority = SnsPriority.HIGH;
			snsMediaDownloadInfo.urlType = SnsUrlType.MMSNS_URL_CDN;
			snsMediaDownloadInfo.snsObjID = 1uL;
			snsMediaDownloadInfo.string_0 = imgUrl;
			SnsMediaMgr.getImg(snsMediaDownloadInfo, true);
			return snsMediaDownloadInfo;
		}
		public static TXNews parseNewsXml(string strXml)
		{
			if (string.IsNullOrEmpty(strXml))
			{
				return null;
			}
			if (!strXml.StartsWith("<"))
			{
				Log.smethod_5("NewsMgr", "strXml need cut");
				int num = strXml.IndexOf("<", 0);
				if (num < 0)
				{
					return null;
				}
				strXml = strXml.Substring(num);
			}
			if (string.IsNullOrEmpty(strXml))
			{
				return null;
			}
			TXNews tXNews = new TXNews();
			TXNews result;
			try
			{
				XDocument xDocument = XDocument.Parse(strXml);
				XElement xElement = xDocument.Element("mmreader");
				if (xElement != null)
				{
					XElement xElement2 = xElement.Element("category");
					if (xElement2 != null)
					{
						tXNews.type = xElement2.Attribute("type").get_Value();
						XElement xElement3 = xElement2.Element("topnew");
						if (xElement3 != null)
						{
							XElement xElement4 = xElement3.Element("cover");
							if (xElement4 != null)
							{
								tXNews.strCoverImgUrl = xElement4.get_Value();
							}
							XElement xElement5 = xElement3.Element("width");
							if (xElement5 != null)
							{
								tXNews.nWidth = int.Parse(xElement5.get_Value());
							}
							XElement xElement6 = xElement3.Element("height");
							if (xElement6 != null)
							{
								tXNews.nHeight = int.Parse(xElement6.get_Value());
							}
							XElement xElement7 = xElement3.Element("digest");
							if (xElement7 != null)
							{
								tXNews.strDescipt = xElement7.get_Value();
							}
						}
						XElement xElement8 = xElement2.Element("item");
						if (xElement8 == null)
						{
							xElement8 = xElement2.Element("newitem");
						}
						if (xElement8 != null)
						{
							XElement xElement9 = xElement8.Element("title");
							if (xElement9 != null)
							{
								tXNews.strTitle = xElement9.get_Value();
							}
							XElement xElement10 = xElement8.Element("url");
							if (xElement10 != null)
							{
								tXNews.strUrl = xElement10.get_Value();
							}
							XElement xElement11 = xElement8.Element("shorturl");
							if (xElement11 != null)
							{
								tXNews.strShorturl = xElement11.get_Value();
							}
							XElement xElement12 = xElement8.Element("longurl");
							if (xElement12 != null)
							{
								tXNews.strLongurl = xElement12.get_Value();
							}
							XElement xElement13 = xElement8.Element("pub_time");
							if (xElement13 != null)
							{
								tXNews.nPubTime = long.Parse(xElement13.get_Value());
							}
							XElement xElement14 = xElement8.Element("tweetid");
							if (xElement14 != null)
							{
								tXNews.strTweetid = xElement14.get_Value();
							}
							XElement xElement15 = xElement8.Element("sources");
							if (xElement15 != null)
							{
								XElement xElement16 = xElement15.Element("source");
								if (xElement16 != null)
								{
									XElement xElement17 = xElement16.Element("name");
									if (xElement17 != null)
									{
										tXNews.strSource = xElement17.get_Value();
									}
									XElement xElement18 = xElement16.Element("icon");
									if (xElement18 != null)
									{
										tXNews.strIconUrl = xElement18.get_Value();
									}
								}
							}
						}
						result = tXNews;
						return result;
					}
				}
				result = null;
			}
			catch (Exception ex)
			{
				Log.smethod_1("NewsMgr", ex.ToString());
				result = null;
			}
			return result;
		}
		private List<ChatMsg> getNewsMsgList(BaseEventArgs evtArgs)
		{
			List<ChatMsg> listObject = evtArgs.getListObject<ChatMsg>();
			if (listObject == null)
			{
				return null;
			}
			List<ChatMsg> list = null;
			using (List<ChatMsg>.Enumerator enumerator = listObject.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					ChatMsg current = enumerator.get_Current();
					if (ContactHelper.isNewsContact(current.strTalker))
					{
						if (list == null)
						{
							list = new List<ChatMsg>();
						}
						list.Add(current);
					}
				}
			}
			return list;
		}
		private void onNewsChange(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs.getListObject<News>() == null)
			{
				return;
			}
			EventCenter.postEvent(EventConst.ON_NEWS_LIST_CHANGE, evtArgs.mObject, null);
		}
		private void ChatUpdateMsg(ChatMsg chatMsg_0, bool isAddMsg = false, int combinMsgCount = 1)
		{
			if (chatMsg_0 == null)
			{
				return;
			}
			chatMsg_0.IsSupport();
		}
		public static void UpdateConversationFromMsg(ChatMsg chatMsg_0, News conv, bool isNewMsg)
		{
			if (chatMsg_0 != null && conv != null)
			{
				conv.nMsgLocalID = chatMsg_0.nMsgLocalID;
				return;
			}
		}
		public static void delMainConversation()
		{
			ConversationMgr.delConversation(ConstantValue.TAG_NEWS);
		}
		public static int getUnReadCountAll()
		{
			List<QConversation> list = StorageMgr.qconv.getList();
			if (list != null && list.get_Count() > 0)
			{
				int num = 0;
				using (List<QConversation>.Enumerator enumerator = list.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						QConversation current = enumerator.get_Current();
						num += current.nUnReadCount;
					}
				}
				return num;
			}
			return 0;
		}
		public static void NewsMsgClearAll()
		{
			StorageMgr.chatMsg.delByTalker(ConstantValue.TAG_NEWS);
			ConversationMgr.delConversation(ConstantValue.TAG_NEWS);
		}
		public static void test()
		{
			for (int i = 0; i < 2; i++)
			{
				ChatMsg chatMsg = new ChatMsg();
				chatMsg.nMsgType = 1;
				chatMsg.nMsgSvrID = 35;
				chatMsg.strTalker = "newsapp";
				chatMsg.strMsg = "<mmreader><category type=\"20\" count=\"1\"><name><![CDATA[腾讯新闻]]></name><topnew><cover><![CDATA[http://pnewsapp.tc.qq.com/newsapp_ls/0/11511_small/0]]></cover><width><![CDATA[640]]></width><height><![CDATA[330]]></height><digest><![CDATA[http://img1.gtimg.com/news/pics/hv1/196/31/1026/66723751.jpghttp://img1.gtimg.com/news/pics/hv1/195/31/1026/66723750.jpg&lt;p&gt;4&#x6708;24&#x65E5;&#xFF0C;&#x5927;&#x8FDE;&#x5468;&#x6C34;&#x5B50;&#x56FD;&#x9645;&#x673A;&#x573A;&#x56E0;&#x5927;&#x96FE;&#x6EDE;&#x7559;&#x65C5;&#x5BA2;5000&#x4F59;&#]]></digest></topnew><item><title><![CDATA[大连机场5千人滞留 美女热舞安抚]]></title><url><![CDATA[http://yd.weixin.qq.com/reader/20/0B6fnV?c=50&n=0&id=12398&aid=WXI2012042500120010&mmuin=1103743700&cv={CLIVER}]]></url><shorturl><![CDATA[http://url.cn/0B6fnV]]></shorturl><longurl><![CDATA[http://weixin.qq.com/a/20120425/001200.htm]]></longurl><pub_time>1335353454</pub_time><cover><![CDATA[http://pnewsapp.tc.qq.com/newsapp_ls/0/11511_small/0|640|330]]></cover><tweetid>I2012042500120010</tweetid><sources><source><name><![CDATA[版权图片，请勿转载]]></name></source></sources></item></category></mmreader>";
				StorageMgr.chatMsg.addMsg(chatMsg);
			}
		}
	}
}
